package com.gopay.bis.ipay.expay.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.ipay.expay.operation.IpayExpayParseMessage;
import com.gopay.common.constants.bank.IpayConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ipay.expay.AuthenBindCard;
import com.gopay.common.domain.bank.ipay.expay.BindCardPay;
import com.gopay.common.domain.bank.ipay.expay.Query;
import com.gopay.common.domain.bank.ipay.expay.Refund;
import com.gopay.common.domain.bank.ipay.expay.UnBindCard;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpTransport;
import com.gopay.remote.bank.ICertHelper;
import com.gopay.remote.bank.businesses.ExPayService;

/**
 * @IpayExPayServiceImpl.java
 * @author jiangxiangjiao
 */
@SuppressWarnings("all")
public class IpayExPayServiceImpl implements ExPayService {

	private static Logger logger = LoggerFactory.getLogger(IpayExPayServiceImpl.class);

	private ICertHelper ipayExpayCerHelper;
	private HttpTransport httpTransport;
	private IpayExpayParseMessage messageParser;

	@Override
	public Object signQuery(BaseMessage baseMsg) throws GopayException {
		return null;
	}

	@Override
	public Object sign(BaseMessage baseMsg) throws GopayException {
		try {
			Map<String, String> param = messageParser.buildReqParam(baseMsg, IpayConstants.AUTH_BIND_CARD);
			httpTransport.setUrl(baseMsg.getPayUrl());
			String response = httpTransport.submit_https(param);
			return messageParser.parseResponse(baseMsg,response,AuthenBindCard.class);
		} catch (Exception e) {
			BankLogUtils.printExPayBindCardExceptionLog(baseMsg, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6053, e);
		}
	}

	@Override
	public Object verify(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object treatyQuery(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object cancel(BaseMessage baseMsg) throws GopayException {
		try {
			Map<String, String> param = messageParser.buildReqParam(baseMsg, IpayConstants.UN_BIND_CARD);
			httpTransport.setUrl(baseMsg.getPayUrl());
			String response = httpTransport.submit_https(param);
			return messageParser.parseResponse(baseMsg,response,UnBindCard.class);
		} catch (Exception e) {
			BankLogUtils.printExPayUnBindCardExceptionLog(baseMsg, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6054, e);
		}
	}

	@Override
	public Object payment(BaseMessage baseMsg) throws GopayException {
		try {
			Map<String, String> param = messageParser.buildReqParam(baseMsg, IpayConstants.BIND_CARD_PAY);
			httpTransport.setUrl(baseMsg.getPayUrl());
			String response = httpTransport.submit_https(param);
			return messageParser.parseResponse(baseMsg,response,BindCardPay.class);
		} catch (Exception e) {
			BankLogUtils.printExPayPaymentExceptionLog(baseMsg, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6057, e);
		}
	}

	@Override
	public Object refund(BaseMessage baseMsg) throws GopayException {
		try {
			Map<String, String> param = messageParser.buildReqParam(baseMsg, IpayConstants.PAY_REFUND);
			httpTransport.setUrl(baseMsg.getPayUrl());
			String response = httpTransport.submit_https(param);
			return messageParser.parseResponse(baseMsg,response,Refund.class);
		} catch (Exception e) {
			BankLogUtils.printRefundExceptionLog(baseMsg, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6087, e);
		}
	}

	@Override
	public Object queryOrder(BaseMessage baseMsg) throws GopayException {
		try {
			Map<String, String> param = messageParser.buildReqParam(baseMsg, IpayConstants.PAY_REFUND_QUERY);
			httpTransport.setUrl(baseMsg.getQueryUrl());
			String response = httpTransport.submit_https(param);
			return messageParser.parseResponse(baseMsg,response,Query.class);
		} catch (Exception e) {
			BankLogUtils.printExPayQueryExceptionLog(baseMsg, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6058, e);
		}
	}

	@Override
	public Object certification(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object sendSms(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object checkSms(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object queryCvn(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	public void setIpayExpayCerHelper(ICertHelper ipayExpayCerHelper) {
		this.ipayExpayCerHelper = ipayExpayCerHelper;
	}

	public void setHttpTransport(HttpTransport httpTransport) {
		this.httpTransport = httpTransport;
	}

	public void setMessageParser(IpayExpayParseMessage messageParser) {
		this.messageParser = messageParser;
	}

}
